/*1*/ package Vista;
/*2*/ import Controlador.*;
/*3*/ import Controlador.ControladorResumen;
/*4*/ import java.awt.Color;
import java.awt.Component;
/*5*/ import java.awt.Point;
/*6*/ import java.awt.event.MouseEvent;
/*7*/ import java.awt.event.MouseMotionAdapter;
/*8*/ import java.awt.event.WindowAdapter;
/*9*/ import java.awt.event.WindowEvent;
/*10*/ import java.io.File;
/*11*/ import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
/*12*/ import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableCellRenderer;
/*13*/ import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
/*14*/

public class JPResumen extends javax.swing.JPanel {

    /*21*/     ControladorResumen control;
    /** Creates new form JPResumen */
    public JPResumen(ControladorResumen controlador) {
/*28*/         this.control = controlador;
        try {
// apariencia del SO
            UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
/*29*/         //seteo de los datos de la tabla
/*30*/         initComponents();
/*31*/         this.setNomCU();
/*32*/         if (this.control.getResumenCU() == null) {
/*33*/             this.setResumen();
/*34*/         } else {
/*35*/             this.setResumenCU(this.control.getResumenCU());
/*36*/         }
/*37*/
/*38*/         //para visualizar el contenido de una celda al pasar el mouse en ella
/*39*/         this.tResumen.addMouseMotionListener(new MouseMotionAdapter() {
/*40*/
/*41*/             @Override
/*42*/             public void mouseMoved(MouseEvent e) {
/*43*/                 Point p = e.getPoint();
/*44*/                 int row = tResumen.rowAtPoint(p);
/*45*/                 int column = tResumen.columnAtPoint(p);
/*46*/                 tResumen.setToolTipText(String.valueOf(tResumen.getValueAt(row, column)));
/*47*/             }
/*48*/         });
/*49*/         //si se cierra la ventana se pide guardar los datos en un archivo
/////////////////*50*/         this.addWindowListener(new WindowAdapter() {
/////////////////*51*/
/////////////////*52*/             @Override
/////////////////*53*/             public void windowClosing(WindowEvent e) {
/////////////////*54*/                 javax.swing.JFileChooser jF1 = new javax.swing.JFileChooser();
/////////////////*55*/                 String ruta = "";
/////////////////*56*/                 try {
/////////////////*57*/                     if (jF1.showSaveDialog(null) == jF1.APPROVE_OPTION) {
/////////////////*58*/                         ruta = jF1.getSelectedFile().getAbsolutePath();
/////////////////*59*/                         if (new File(ruta).exists()) {
/////////////////*60*/                             if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(null, "El fichero existe,desea reemplazarlo?", "Titulo", JOptionPane.YES_NO_OPTION)) {
/////////////////*61*/                                 control.archivoSesion(ruta);
/////////////////*62*/                             }
/////////////////*63*/                         } else {
/////////////////*64*/                             control.archivoSesion(ruta);
/////////////////*65*/                         }
/////////////////*66*/                     }
/////////////////*67*/                 } catch (Exception ex) {
/////////////////*68*/                     ex.printStackTrace();
/////////////////*69*/                 }
/////////////////*70*/
/////////////////*71*/                 System.exit(0);
/////////////////*72*/             }
/////////////////*73*/         });
/*74*/     }
/*75*/
/*76*/     //seteo de los resumenes de una sesion anterior
/*77*/     /**
     * Seteo de los resumenes de una sesion anterior
     * @param resumen
     */
/*81*/     public void setResumenCU(Vector resumen) {
/*82*/         for (int i = 0; i < resumen.size(); i++) {
/*83*/             this.tResumen.setValueAt(resumen.get(i), i, 2);
                     Integer intObj = new Integer(i+1);   //agrega el numero a la fila
                          this.tResumen.setValueAt(intObj, i, 0);
/*84*/         }
/*85*/     }
/*86*/
/*87*/     //seteo del nombte de los casos de uso
/*88*/     /**
     * Seteo del nombre de los casos de uso
     */
/*91*/     public void setNomCU() {
/*92*/         Vector nomCU = new Vector();
/*93*/         nomCU = this.control.getNombreCasoUso();
/*94*/         this.setFilas(nomCU.size());
/*95*/         for (int i = 0; i < nomCU.size(); i++) {
/*96*/             this.tResumen.setValueAt(nomCU.get(i), i, 1);
/*97*/         }
/*98*/     }
/*99*/
/*100*/     //seteo de los resumenes en la tabla
/*101*/     /**
     * Seteo de los resumenes en la tabla
     */
/*104*/     public void setResumen() {
/*105*/         Vector resumenON = new Vector();
/*106*/         resumenON = this.control.getResumen();
/*107*/         for (int i = 0; i < resumenON.size(); i++) {
/*108*/             this.tResumen.setValueAt(resumenON.get(i), i, 2);
                 Integer intObj = new Integer(i+1);   //agrega el numero a la fila
                          this.tResumen.setValueAt(intObj, i, 0);
/*109*/         }
/*110*/     }
/*111*/
/*112*/     //seteo de las propiedades de la tabla resumen
/*113*/     /**
     * Seteo de las propiedades de la tabla resumen
     * @param tam indica el tamaño de las filas seteadas
     */
/*117*/     public void setFilas(int tam) {
/*118*/         DefaultTableModel tabla = new MyTableModel(new Object[][]{},
/*119*/                 new String[]{
/*120*/                     "Número","Caso de Uso", "Resumen"
/*121*/                 });
/*122*/         tabla.setRowCount(tam);
/*123*/         tabla.setColumnCount(3);
/*124*/         this.tResumen.setModel(tabla);
                this.tResumen.getColumnModel().getColumn(2).setCellRenderer(new TextAreaRenderer());
/*125*/         this.tResumen.setShowGrid(true);
                TableColumn col= tResumen.getColumn("Número");
                col.setMaxWidth(50);
/*126*/         this.tResumen.setGridColor(Color.black);

                tResumen.getColumn("Resumen").setCellRenderer(new DefaultTableCellRenderer() {
                    @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                setText(value.toString());
                 Icon ico = new ImageIcon(getClass().getResource("/Fotos/lapiz.png"));
                 setIcon(ico);
               //  setBackground(Color.LIGHT_GRAY);
                 table.getTableHeader().setReorderingAllowed(false);
                return this;
                }
            });
/*127*/     }
/*128*/
/*129*/     /**
             *
             */
/*132*/     class MyTableModel extends DefaultTableModel {
/*133*/
/*134*/         /**
         * Modelo de tabla
         * @param data
         * @param columnNames
         */
/*139*/         public MyTableModel(Object[][] data, Object[] columnNames) {
/*140*/             super(data, columnNames);
/*141*/         }
/*142*/
/*143*/               /**
        * Verificar si alguna de las celdas que se tienen son editables
        */
/*149*/         @Override
/*150*/         public boolean isCellEditable(int row, int col) {
/*151*/             if (col == 0 || col == 1) {
/*152*/                 return false;
/*153*/             } else {
/*154*/                 return true;
/*155*/             }
/*156*/         }
/*157*/     }
/*158*/
/*159*/     //obtener los resumenes modificados de la tabla
/*160*/         /**
     * Obtener los resumenes modificados de la tabla
     * @return devuelve el resumen de los casos de uso
     */
/*164*/     public Vector resumenCU() {
/*165*/         Vector resumenCU = new Vector();
/*166*/         for (int i = 0; i < this.tResumen.getRowCount(); i++) {
/*167*/             resumenCU.add(this.tResumen.getValueAt(i, 2));
/*168*/         }
/*169*/         return resumenCU;
/*170*/     }
/*171*/

   
/*172*/     //verificar si hay nulos
/*173*/     /**
     * Verificar si hay nulos
     * @return los vectores que son nulos (para el caso de los resúmenes)
     */
/*177*/     public Vector nulos() {
/*178*/         Vector v = new Vector();
/*179*/         for (int i = 0; i < this.tResumen.getRowCount(); i++) {
/*180*/             if (this.tResumen.getValueAt(i, 2) == null) {
/*181*/                 v.add(0, 1);
/*182*/                 v.add(1, i + 1);
/*183*/                 return v;
/*184*/             }
/*185*/         }
/*186*/         v.add(0, 0);
/*187*/         return v;
/*188*/     }
/*189*/
/*190*/     //verificar si hay celdas seleccionadas
/*191*/     /**
     * Verificar si hay celdas seleccionadas
     * @return Retorna si la celda ha sido seleccionada (1) o no (0)
     */
/*195*/     public int select() {
/*196*/         for (int i = 0; i < this.tResumen.getRowCount(); i++) {
/*197*/             if (this.tResumen.isCellSelected(i, 2)) {
/*198*/                 return 1;
/*199*/             }
/*200*/         }
/*201*/         return 0;
/*202*/     }


            public void alerta(String mensaje)
            {
            JOptionPane.showMessageDialog(null, mensaje, "¡Advertencia!", JOptionPane.WARNING_MESSAGE);
            }
         /**
         * Verifica si las celdas de la columna "Resumen" estan completas.  
         * @return Boolean, true, si todas las celdas se encuentran con descripción y false de otra forma
         */
        public boolean checkRcompletos() {
            String resumen="";
            for (int i = 0; i < this.tResumen.getRowCount(); i++) {
             resumen=(String)this.tResumen.getValueAt(i, 2);

                if (resumen.compareTo("")==0)return false;
            }
            return true;
        }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tResumen = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(958, 589));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(proyectodetitulo.ProyectoDeTituloApp.class).getContext().getResourceMap(JPResumen.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(958, 589));
        jPanel1.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                BotonDeshacer(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tResumen.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Caso de Uso", "Resumen"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tResumen.setToolTipText(resourceMap.getString("tResumen.toolTipText")); // NOI18N
        tResumen.setCellSelectionEnabled(true);
        tResumen.setName("tResumen"); // NOI18N
        jScrollPane1.setViewportView(tResumen);
        tResumen.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        tResumen.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tResumen.columnModel.title0")); // NOI18N
        tResumen.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tResumen.columnModel.title1")); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel8.setIcon(resourceMap.getIcon("jLabel8.icon")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 743, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(193, 193, 193))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 270, Short.MAX_VALUE)
                        .addComponent(jLabel8)
                        .addGap(285, 285, 285))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 936, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel1)
                .addGap(55, 55, 55)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(205, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 962, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    public void deshacer(){
         Vector resumenON = new Vector();
         resumenON = this.control.getResumen();
         for (int i = 0; i < resumenON.size(); i++) {
             if (this.tResumen.isCellSelected(i, 2)){
             this.tResumen.setValueAt(resumenON.get(i), i, 2);
            }
         }
    }
    private void BotonDeshacer(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_BotonDeshacer
        // TODO add your handling code here:
    }//GEN-LAST:event_BotonDeshacer


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tResumen;
    // End of variables declaration//GEN-END:variables

}
